package org.carabiner.infinitest.swingui;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.swing.SwingUtilities;

import org.carabiner.infinitest.TestEvent;
import org.carabiner.infinitest.TestStatusListener;

/**
 * 
 * @author <a href="mailto:benrady@gmail.com"Ben Rady</a>
 */
class EventSupport
{
  private EventSupport(){}
  
  /**
   * Ensures that test events are dispatched on the event thread
   */
  public static TestStatusListener eventNormalizer(final TestStatusListener listener)
  {
    return (TestStatusListener) Proxy.newProxyInstance(listener.getClass().getClassLoader(), new Class[] { TestStatusListener.class }, new InvocationHandler()
    {
      public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable
      {
        if (SwingUtilities.isEventDispatchThread())
          method.invoke(listener, args);
        else
          SwingUtilities.invokeLater(new Runnable()
          {
            public void run()
            {
              try
              {
                method.invoke(listener, args);
              }
              catch (IllegalAccessException e)
              {
                throw new RuntimeException(e);
              }
              catch (InvocationTargetException e)
              {
                throw new RuntimeException(e);
              }
            }
          }); 
        return null;
      }
    });
  }
}
